package utils

import (
	"database/sql"
	"gin-epg/internal/app/entity"
	"golang.org/x/net/html"
	"log"
	"strings"
	"time"
)

func HasAttr(n *html.Node, key, value string) bool {
	for _, attr := range n.Attr {
		if attr.Key == key && attr.Val == value {
			return true
		}
	}
	return false
}

func ParseWuyilivetvEpgInfo(n *html.Node, urlDate string, channel string, source string) []entity.EpgProgramCollect {
	var programs []entity.EpgProgramCollect
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if c.Type == html.ElementNode && c.Data == "li" {
			var startTime, title string
			for child := c.FirstChild; child != nil; child = child.NextSibling {
				if child.Type == html.ElementNode && child.Data == "span" {
					if HasAttr(child, "class", "time") {
						for grandChild := child.FirstChild; grandChild != nil; grandChild = grandChild.NextSibling {
							if grandChild.Type == html.ElementNode && grandChild.Data == "a" {
								startTime = grandChild.FirstChild.Data
							}
						}
					}
				} else if child.Type == html.ElementNode && child.Data == "a" {
					if HasAttr(child, "class", "name link") {
						title = child.FirstChild.Data
					}
				}
			}
			if startTime != "" && title != "" {
				startTime = urlDate + "T" + startTime + ":00+08:00"
				momentStartDate, err := time.Parse("2006-01-02T15:04:05-07:00", startTime)
				if err != nil {
					log.Printf("Error parsing time: %v", err)
					continue
				}
				start := momentStartDate.Format("15:04")
				programs = append(programs, entity.EpgProgramCollect{
					Title:   title,
					Source:  source,
					Channel: channel,
					Date:    entity.NullDate{sql.NullTime{Time: time.Date(momentStartDate.Year(), momentStartDate.Month(), momentStartDate.Day(), 0, 0, 0, 0, time.Local), Valid: true}},
					Start:   start,
				})
			}
		}
	}
	return programs
}

func ParseSports8EpgInfo(n *html.Node, urlDate string, channel string, source string) []entity.EpgProgramCollect {
	var programs []entity.EpgProgramCollect
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if c.Type == html.ElementNode && c.Data == "div" {
			// 这里处理早间、午间、晚间等时间段的 div
			programs = parseTimeSlot(c, urlDate, channel, source, programs)
		}
	}
	return programs
}

func parseTimeSlot(n *html.Node, urlDate string, channel string, source string, programs []entity.EpgProgramCollect) []entity.EpgProgramCollect {
	var startTime string
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if c.Type == html.ElementNode && c.Data == "p" {
			title := ""
			for _, attr := range c.Attr {
				if attr.Key == "class" && strings.Contains(attr.Val, "pc_") {
					for child := c.FirstChild; child != nil; child = child.NextSibling {
						if child.Type == html.ElementNode && child.Data == "em" {
							startTime = child.FirstChild.Data
						} else if child.Type == html.TextNode {
							title += child.Data
						}
					}
					if startTime != "" && title != "" {
						// 去除 title 末尾的空格
						//title = strings.Trim(title, " ")
						// 转换 &nbsp; 并去除 title 末尾的空格
						title = html.UnescapeString(title)
						title = strings.TrimSpace(title)

						startTime = urlDate + "T" + startTime + ":00+08:00"
						momentStartDate, err := time.Parse("2006-01-02T15:04:05-07:00", startTime)
						if err != nil {
							log.Printf("Error parsing time: %v", err)
							continue
						}
						start := momentStartDate.Format("15:04")
						programs = append(programs, entity.EpgProgramCollect{
							Title:   title,
							Source:  source,
							Channel: channel,
							Date:    entity.NullDate{sql.NullTime{Time: time.Date(momentStartDate.Year(), momentStartDate.Month(), momentStartDate.Day(), 0, 0, 0, 0, time.Local), Valid: true}},
							Start:   start,
						})
					}
				}
			}
		}
	}
	return programs
}
